from django.db import models
from django.contrib.auth.models import User
from django.utils.safestring import mark_safe
from django.conf import settings

#####################################################################################
# Presentation models
#####################################################################################
class new(models.Model):
    title = models.CharField(max_length=300, blank=True)
    data = models.TextField(max_length=3000, blank=True)
    #system data
    creation_date = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return 'id:'+str(self.id)

#####################################################################################
# Real objects
#####################################################################################
class person(models.Model):
    #registration data
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50, blank=True)
    organization = models.CharField(max_length=100, blank=True)
    email = models.EmailField(max_length=75, unique=True)
    password = models.CharField(max_length=50)
    #ps data
    spent_flops = models.FloatField(default=0, blank=True)
    cpu_flops = models.FloatField(default=0, blank=True)
    #system data
    key = models.CharField(max_length=100, unique=True, blank=True)
    user = models.ForeignKey(User, unique=True)
    creation_date = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return self.name

    def get_fullname(self):
        return self.name+' '+self.surname

    def get_count_nodes(self):
        return node.objects.filter(person=self.id).count()

    def get_count_results(self):
        return ticket.objects.filter(person = self.id).count()

    def get_spent_flops(self):
        return self.spent_flops

    def get_earned_flops(self):
        return self.cpu_flops

    def get_points(self):
        return self.cpu_flops - self.spent_flops

    def get_key(self):
        return '%(key)s$%(id)s$%(url)s' % {'key':self.key, 'id':self.id, 'url':settings.NODE_LINK}

class node(models.Model):
    #registration data
    caption = models.CharField(max_length=100, blank=True)
    person = models.ForeignKey(person)
    #ps data
    cpu_flops = models.FloatField(default=0)
    cpu_number = models.IntegerField(default=0)
    cpu_power =  models.FloatField(default=0)
    platform = models.CharField(max_length=250)
    #system data
    online = models.DateTimeField(auto_now=True)
    key = models.CharField(max_length=100, unique=True)
    creation_date = models.DateTimeField(auto_now=True)

    def __unicode__(self):
        return 'id:'+str(self.id)

    def get_caption(self):
        if self.caption:
            return self.caption
        return 'node'+str(self.id)

#####################################################################################
# Mutable objects
#####################################################################################
class dataset(models.Model):
    person = models.ForeignKey(person)
    caption = models.CharField(max_length=100)
    description = models.TextField(max_length = 500, blank = True)
    filesize = models.FloatField(default=0)
    #system data
    creation_date = models.DateTimeField(auto_now=True)
    def __unicode__(self):
        return str(self.id)+' - '+self.caption

class mapreduce(models.Model):
    caption = models.CharField(max_length=100, unique=True)
    description = models.TextField(max_length = 3000, blank = True)
    link = models.CharField(max_length=100)
    def __unicode__(self):
        return str(self.id)+' - '+self.caption

#####################################################################################
# Logical objects
#####################################################################################
class ticket(models.Model):
    #registration data
    person = models.ForeignKey(person)
    mapreduce = models.ForeignKey(mapreduce)
    data = models.ForeignKey(dataset)
    #ps data
    status = models.IntegerField(blank = True, default=-1)
    result_file = models.CharField(blank = True, max_length=100)
    result_data = models.TextField(blank = True, max_length = 40000)
    #system data
    mapped = models.BooleanField(default=False)
    completed = models.BooleanField(default=False)
    completion_date = models.DateTimeField(auto_now = True)
    creation_date = models.DateTimeField(auto_now = True)
    spent_flops = models.FloatField(default=0, blank=True)

    def __unicode__(self):
        return 'id:'+str(self.id)

    def results_to_str(self):
        return '<b>%d</b> - %.2f  MB <a href="/view/result/%d">download</a>' % (self.id, len(self.result_data)/(1024.0**2),self.id)

class unit(models.Model):
    #registration data
    node = models.ForeignKey(node, null = True)
    ticket = models.ForeignKey(ticket)
    #ps data
    fitness = models.FloatField(default=0, blank=True)
    meta = models.TextField(max_length = 20000, blank = True)
    network = models.TextField(max_length = 20000, blank = True)

    status = models.IntegerField(blank = True, default=-1)
    #system data
    creation_date = models.DateTimeField(auto_now = True)
    completeion_date = models.DateTimeField(null = True)

    def __unicode__(self):
        return 'id:'+str(self.id)

#####################################################################################
# Custom queries
#####################################################################################
def total_flops():
    from django.db import connection
    cursor = connection.cursor()
    cursor.execute("SELECT sum(cpu_flops) FROM main_node;")
    row = cursor.fetchone()
    return row[0]

#####################################################################################
# Enumerations
#####################################################################################
STATUS={'created':-1,'fetched': 0, 'uploaded': 1, 'done': 1000}